//
//  WBEmoticonManager.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/13.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//

import UIKit
let WBEmoticonCol: Int = 7
let WBEmoticonRow: Int = 3
let WBEmoticonNum: Int = WBEmoticonCol * WBEmoticonRow - 1
class WBEmoticonManager: NSObject {
    
    //定义成静态型的表情数组 让返回的都是同一个
    //最近表情数组
    static var recenEmotion: [WBEmoticon] = [WBEmoticon]()
    //默认表情数组
    static var  defaultEmotion: [WBEmoticon] = WBEmoticonManager.getEmotion("com.sina.default")
    
    //emoji表情
    static var emojiEmotion: [WBEmoticon] =  WBEmoticonManager.getEmotion("com.apple.emoji")
  
    //浪小花表情
    static var lxhEmotion: [WBEmoticon] = WBEmoticonManager.getEmotion("com.sina.lxh")
    
    
    //读取图片文件的方法
    class  func getEmotion(package: String) ->[WBEmoticon] {
        //1.获取budie文件路径
        let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        //2.拼接文件路径
        let packPath = (bundlePath as NSString).stringByAppendingPathComponent(package)
        //3. 包文件路径
        let infoPath = (packPath as NSString).stringByAppendingPathComponent("info.plist")
        //4加载plist
        let dic = NSDictionary(contentsOfFile: infoPath)!
        //print(infoPath)
        //5. 字典转模型
        //5.1 先emotions 数组
        let emoticonDic = dic["emoticons"] as! [[String: AnyObject]]
        //5.2 定义一个临时数组来保存 表情模型
        var tamp: [WBEmoticon] = [WBEmoticon]()
        //6遍历字典
        for emoticonDic in emoticonDic {
          let emoticon = WBEmoticon(dic: emoticonDic)
//           MARK: 把包的路径名负责给模型
            emoticon.package = packPath
           tamp.append(emoticon)
            //print(tamp.count)
        }
        return tamp
    }
    
    
    //MARK:- 二维数组 分割成一个二维数组
    class func pageEmotion(emoticons: [WBEmoticon]) -> [[WBEmoticon]] {
        //数组进行分割 
        let page = (emoticons.count - 1) / WBEmoticonNum + 1
         //定义一个临时数组来保存分割出来的二维数组
        var temp: [[WBEmoticon]] = [[WBEmoticon]]()
        
        //循环进行分割
        for i in 0..<page {
          let loc = i * WBEmoticonNum
            var len = WBEmoticonNum
            if len + loc > emoticons.count {
               len = emoticons.count - loc
            }
            //转换成oc数组来实现分割
            let subEmotion = (emoticons as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [WBEmoticon]
            //把分割好的一维数组 放到二维数组
            temp.append(subEmotion)
        }
        
         return temp
       }
      //MARK: - 定义方法 返回表情包数组
    ///返回三维数组
    class func getAllEmoticon() ->[[[WBEmoticon]]]{
      return [pageEmotion(recenEmotion),
              pageEmotion(defaultEmotion),
               pageEmotion(emojiEmotion),
                pageEmotion(lxhEmotion)]
    }
    
//MARK: 添加最近表情的方法 
    class func addRecent(emotion: WBEmoticon) {
       //判断是否已经有重复表情
        if !recenEmotion.contains(emotion) {
            //插入到最前面
            recenEmotion.insert(emotion, atIndex: 0)
          //recenEmotion.append(emotion)
          
        }
    
        if recenEmotion.count > 20 {
        
          recenEmotion.removeLast()
        }
    }
    
      //返回表情中的内容
    class func searchEmoticon(text: String) ->WBEmoticon? {
       //循环遍历表情包
        for emoticon in WBEmoticonManager.defaultEmotion {//默认表情
            if emoticon.chs == text {
            
               return emoticon
            }
        }
        
        for emoticon in WBEmoticonManager.lxhEmotion { //浪小花
           
            if emoticon.chs == text {
            return  emoticon
        }
        }
        
        return nil
    }
}


